import java.util.Arrays;//导包的快捷键--Alt+回车
import java.util.Comparator;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User:恋恋
 * Date:2022-04-12
 * Time:9:31
 */
//点入某个函数-》按住Ctrl+点击函数
class Student implements Comparable<Student>{//普通类继承Comparable的接口---implements
    int age;
    String name;
    int core;

    public Student(int age, String name, int core) {//利用构造方法来初始化成员变量
        this.age = age;
        this.name = name;
        this.core = core;
    }

    @Override
    public String toString() {
        return "Student{" +
                "age=" + age +
                ", name='" + name + '\'' +
                ", core=" + core +
                '}';
    }

    @Override//普通类继承接口需重写所有抽象方法
    public int compareTo(Student student) {//但是这个太固定了--不方便
        //基本数据类型---数据减去的结果大于返回1；小于返回-1；等于返回0---》从小到大
       // return this.age-student.age;
        //引用数据类型---使用compareTO函数--从小到大
       return student.name.compareTo(this.name);
    }
    public static void sort(Comparable[] students){//利用冒泡排序
        for (int i = 0; i < students.length-1 ; i++) {//趟数--总元素-1
            for (int j = 0; j < students.length-1-i ; j++) {//每一趟的对数--每一趟的趟数是前一趟的对数-1
                if(students[j-1].compareTo(students[j])>0){
                    Comparable temp=students[j];
                    students[j]=students[j-1];
                    students[j-1]=temp;
                }
            }

        }
    }
}
//引错接口了---comparator
/*class AgeComparator implements Comparable<Student>{
    //只有这个是抽象方法
    @Override
    public int compare(Student o1,Student o2) {
        return o1.age-o2.age;
    }
}*/
//继承Comparator接口，分别对应比较基本数据类型的接口和比较引用数据类型的接口
class AgeComparator implements Comparator<Student>{
    @Override
    public int compare(Student t2, Student t1) {
        return t2.age-t1.age;
    }
}
class NameComparator implements Comparator<Student>{
    @Override
    public int compare(Student t2, Student t1) {
        return t1.name.compareTo(t2.name);
    }
}

public class TestDome {
    //方法三：利用Comparable中的CompareTo方法

    public static void main(String[] args) {
        Student[] students=new Student[3];//创建Comparable的数组，数组中的每个元素都可以实现Comparable方法
        students[0]=new Student(21,"耿米芬",15);
        students[1]=new Student(19,"周可心",10);
        students[2]=new Student(20,"陈恋",29);
        Student.sort(students);
        System.out.println(Arrays.toString(students));
    }
    //方法二：实例化基本数据类型的接口和引用数据类型的接口，在Arrays.sort(数组，实例化的接口对象）
    public static void main2(String[] args) {
        Student[] students=new Student[3];//关于类的数组
        students[0]=new Student(21,"耿米芬",15);
        students[1]=new Student(19,"周可心",10);
        students[2]=new Student(20,"陈恋",29);
        AgeComparator ageComparator=new AgeComparator();
        NameComparator nameComparator=new NameComparator();
        System.out.println("排序前"+ Arrays.toString(students));
        Arrays.sort(students,ageComparator);//将比较构造器作为我的第二个参数
        System.out.println("排序后"+Arrays.toString(students));
        Arrays.sort(students,nameComparator);
        System.out.println("排序后"+Arrays.toString(students));
    }
    //方法一：利用调用接口Comparable,调用compareTo方法
    public static void mai1(String[] args) {
        Student[] students=new Student[3];//关于类的数组
        students[0]=new Student(21,"耿米芬",15);
        students[1]=new Student(19,"周可心",10);
        students[2]=new Student(20,"陈恋",29);
      /*  if(students[0]>students[1]){//在java中引用对象不能用比较(><=），只能用compareTO比较
            System.out.println("大滴");
        }*/
        if(students[0].compareTo(students[1])>0){
            System.out.println("大滴");
        }

        System.out.println("排序前"+ Arrays.toString(students));
        Arrays.sort(students);//用该函数对数组进行排序-----运行错误（编译器不知道以哪个来进行比较）---》ClassCastException类型转化异常
        System.out.println("排序后"+Arrays.toString(students));
    }


}
